package flc.ast.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.databinding.ActivitySelPictureBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelPictureActivity extends BaseAc<ActivitySelPictureBinding> {

    public static int type;
    private SelPictureAdapter pictureAdapter;
    private String selPath = "";
    private List<String> listSel = new ArrayList<>();
    private int oldPosition;
    private String again = "";


    @Override
    protected int onCreate() {
        return R.layout.activity_sel_picture;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivSelPictureBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvConfirm.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new SelPictureAdapter();
        mDataBinding.rvPictureList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
        //批量压缩再次进入选择
        again = getIntent().getStringExtra(Content.Again);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvConfirm://确定
                if (selPath.isEmpty() && listSel.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_pic));
                    return;
                }
                if (type == 0) {
                    PictureCompressActivity.imgPath = selPath;
                    startActivity(PictureCompressActivity.class);
                } else if (type == 1) {
                    AppointCompressActivity.imgPath = selPath;
                    startActivity(AppointCompressActivity.class);
                } else if (type == 2) {
                    IDPicCompressActivity.imgPath = selPath;
                    startActivity(IDPicCompressActivity.class);
                } else if (type == 3) {
                    if (TextUtils.isEmpty(again)) {
                        BatchCompressActivity.listPath = listSel;
                        startActivity(BatchCompressActivity.class);
                    } else {
                        //添加图片
                        Intent intent = new Intent();
                        intent.putExtra(Content.imagePath, selPath);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                } else if (type == 4) {
                    ChangeSizeActivity.imgPath = selPath;
                    startActivity(ChangeSizeActivity.class);
                } else if (type == 5) {
                    if (TextUtils.isEmpty(again)) {
                        FreeSpliceActivity.listPath = listSel;
                        startActivity(FreeSpliceActivity.class);
                    } else {
                        //添加图片
                        Intent intent = new Intent();
                        intent.putExtra(Content.imagePath, selPath);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                } else if (type == 6) {
                    LongSpliceActivity.listPath = listSel;
                    startActivity(LongSpliceActivity.class);
                } else if (type == 7) {
                    PicMosaicActivity.imgPath = selPath;
                    startActivity(PicMosaicActivity.class);
                } else if (type == 8) {
                    if (TextUtils.isEmpty(again)) {
                        PicOverActivity.imgPath = selPath;
                        startActivity(PicOverActivity.class);
                    } else {
                        //添加图片
                        Intent intent = new Intent();
                        intent.putExtra(Content.imagePath, selPath);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                } else if (type == 9) {
                    PicWaterMarkActivity.imgPath = selPath;
                    startActivity(PicWaterMarkActivity.class);
                }else if (type == 10) {
                    PicGridActivity.imgPath = selPath;
                    startActivity(PicGridActivity.class);
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (type == 0 || type == 1 || type == 2 || type == 4 || type == 7 || type == 8 || type == 9 || type == 10) {
            pictureAdapter.getItem(oldPosition).setChecked(false);
            pictureAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            pictureAdapter.getItem(position).setChecked(true);
            pictureAdapter.notifyItemChanged(position);
            selPath = pictureAdapter.getItem(position).getPath();
        } else if (type == 3) {
            if (TextUtils.isEmpty(again)) {
                if (pictureAdapter.getItem(position).isChecked()) {
                    pictureAdapter.getItem(position).setChecked(false);
                    listSel.remove(pictureAdapter.getItem(position).getPath());
                } else {
                    pictureAdapter.getItem(position).setChecked(true);
                    listSel.add(pictureAdapter.getItem(position).getPath());
                }
                pictureAdapter.notifyItemChanged(position);
                mDataBinding.tvConfirm.setText(getString(R.string.confirm_num, listSel.size()));
            } else {
                pictureAdapter.getItem(oldPosition).setChecked(false);
                pictureAdapter.notifyItemChanged(oldPosition);
                oldPosition = position;
                pictureAdapter.getItem(position).setChecked(true);
                pictureAdapter.notifyItemChanged(position);
                selPath = pictureAdapter.getItem(position).getPath();
            }
        } else if (type == 5) {
            if (TextUtils.isEmpty(again)) {
                if (pictureAdapter.getItem(position).isChecked()) {
                    pictureAdapter.getItem(position).setChecked(false);
                    listSel.remove(pictureAdapter.getItem(position).getPath());
                } else {
                    if (listSel.size() == 9) {
                        ToastUtils.showShort(getString(R.string.max_sel_nine));
                        return;
                    }
                    pictureAdapter.getItem(position).setChecked(true);
                    listSel.add(pictureAdapter.getItem(position).getPath());
                }
                pictureAdapter.notifyItemChanged(position);
                mDataBinding.tvConfirm.setText(getString(R.string.confirm_num, listSel.size()));
            } else {
                pictureAdapter.getItem(oldPosition).setChecked(false);
                pictureAdapter.notifyItemChanged(oldPosition);
                oldPosition = position;
                pictureAdapter.getItem(position).setChecked(true);
                pictureAdapter.notifyItemChanged(position);
                selPath = pictureAdapter.getItem(position).getPath();
            }
        } else {
            if (pictureAdapter.getItem(position).isChecked()) {
                pictureAdapter.getItem(position).setChecked(false);
                listSel.remove(pictureAdapter.getItem(position).getPath());
            } else {
                pictureAdapter.getItem(position).setChecked(true);
                listSel.add(pictureAdapter.getItem(position).getPath());
            }
            pictureAdapter.notifyItemChanged(position);
            mDataBinding.tvConfirm.setText(getString(R.string.confirm_num, listSel.size()));
        }
    }

    @Override
    protected void initData() {
        if (type == 0 || type == 1 || type == 2 || type == 4 || type == 7 || type == 8 || type == 9 || type == 10) {
            mDataBinding.tvConfirm.setText(getString(R.string.confirm_text));
        } else if (type == 3) {
            if (TextUtils.isEmpty(again)) {
                mDataBinding.tvConfirm.setText(getString(R.string.confirm_num, listSel.size()));
            } else {
                mDataBinding.tvConfirm.setText(getString(R.string.confirm_text));
            }
        } else if (type == 5) {
            if (TextUtils.isEmpty(again)) {
                mDataBinding.tvConfirm.setText(getString(R.string.confirm_num, listSel.size()));
            } else {
                mDataBinding.tvConfirm.setText(getString(R.string.confirm_text));
            }
        } else if (type == 6) {
            mDataBinding.tvConfirm.setText(getString(R.string.confirm_num, listSel.size()));
        }
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                    mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}